﻿#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：ClassExtention
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/6 19:26:15
*******************************************************************
* Copyright @ henrychang 2019. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using System;

public static class ClassExtention
{
}

public static class FuncOrActionOrEventExtension
{
    #region Func Extension

    public static T InvokeGracefully<T>(this Func<T> selfFunc)
    {
        return null != selfFunc ? selfFunc() : default(T);
    }

    #endregion Func Extension

    #region Action

    /// <summary>
    /// Call action
    /// </summary>
    /// <param name="selfAction"></param>
    /// <returns> call succeed</returns>
    public static bool InvokeGracefully(this System.Action selfAction)
    {
        if (null != selfAction)
        {
            selfAction();
            return true;
        }
        return false;
    }

    /// <summary>
    /// Call action
    /// </summary>
    /// <param name="selfAction"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static bool InvokeGracefully<T>(this Action<T> selfAction, T t)
    {
        if (null != selfAction)
        {
            selfAction(t);
            return true;
        }
        return false;
    }

    /// <summary>
    /// Call action
    /// </summary>
    /// <param name="selfAction"></param>
    /// <returns> call succeed</returns>
    public static bool InvokeGracefully<T, K>(this Action<T, K> selfAction, T t, K k)
    {
        if (null != selfAction)
        {
            selfAction(t, k);
            return true;
        }
        return false;
    }

    /// <summary>
    /// Call delegate
    /// </summary>
    /// <param name="selfAction"></param>
    /// <returns> call suceed </returns>
    public static bool InvokeGracefully(this Delegate selfAction, params object[] args)
    {
        if (null != selfAction)
        {
            selfAction.DynamicInvoke(args);
            return true;
        }
        return false;
    }

    #endregion Action
}